package com.cty.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: cty
 * @Date: 2020/3/16 15:51
 * @Description:  拦截器
 * @version: 1.0
 */
public class DemoInterceptor2 implements HandlerInterceptor {
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    // 进入控制器之前执行
    // 若返回值为false，阻止进入控制器
    // 内部主要编写控制代码（什么请求可以访问）
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("1-2-执行preHandle");
        return true;
    }

    // 控制器执行完成，进入jsp视图前执行
    // 主要用于日志记录和敏感词语过滤
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("3-2-执行postHandle");
    }

    // jsp视图执行完成后执行
    // 记录执行过程中的异常到日志
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("5-2-执行afterCompletion");
    }
}

/* res:
1-执行preHandle
1-2-执行preHandle
2-执行Demo控制器
3-2-执行postHandle
3-执行postHandle
postHandle: 跳转到/demo.jsp
postHandle: model值为: 我爱我的祖国。
postHandle: model修改后的值为: 我爱我的**。
4-执行Demo.jsp
5-2-执行afterCompletion
5-执行afterCompletion
*/
